<template>
    <view class="container">
		<!-- 下拉刷新组件 -->
		<mix-pulldown-refresh ref="mixPulldownRefresh" class="panel-content" :top="0" @refresh="onPulldownReresh" @setEnableScroll="setEnableScroll">
			<!-- 内容部分 -->
			<scroll-view 
				class="panel-scroll-box" 
				:scroll-y="enableScroll" 
				@scrolltolower="loadMore"
				>
				<!-- 
					* 新闻列表 
					* 和nvue的区别只是需要把uni标签转为weex标签而已
					* class 和 style的绑定限制了一些语法，其他并没有不同
				-->
				<view class="news-list">
					<view class="count_result">{{ countResult }}</view>
					<view v-for="(item, index) in newsList" :key="index" class="news-item" @click="navToDetails(item)">
						<text :class="['title', 'title'+item.show_type]">{{item.title}}</text>
						<view v-if="item.images.length > 0" :class="['img-list', 'img-list'+item.show_type, item.images.length === 1 && item.show_type===3 ? 'img-list-single': '']">
							<view 
								v-for="(imgItem, imgIndex) in item.images" :key="imgIndex"
								:class="['img-wrapper', 'img-wrapper'+item.show_type, item.images.length === 1 && item.show_type===3 ? 'img-wrapper-single': '']"
							>
								<image class="img" :src="imgItem"></image>
								<view class="video-tip" v-if="item.videoSrc">
									<image class="video-tip-icon" src=""></image>
								</view>
							</view>
						</view>
						<!-- 空图片占位 -->
						<view v-else class="img-empty"></view>
						<view :class="['bot', 'bot'+item.show_type]">
							<text class="author">{{item.author_name}}</text>
							<text class="time">{{item.create_time}}</text>
						</view>
					</view>
				</view>
				<!-- 上滑加载更多组件 -->
				<mix-load-more :status="loadMoreStatus"></mix-load-more>
			</scroll-view>
		</mix-pulldown-refresh>
    </view>
</template>
<script>
	import mixPulldownRefresh from '@/components/mix-pulldown-refresh/mix-pulldown-refresh';
	import mixLoadMore from '@/components/mix-load-more/mix-load-more';
	let windowWidth = 0, scrollTimer = false, tabBar;
	export default {
		components: {
			mixPulldownRefresh,
			mixLoadMore
		},
        data() {
            return {
				keyword:'',
				newsList: [],
				countResult:'',
				page:1,
				loadMoreStatus:1,
				enableScroll: true
            }
        },
		onLoad: function (option) {
			this.keyword = option.keyword;
			uni.setNavigationBarTitle({
				title: '搜索：'+this.keyword
			});
			this.loadNewsList('add');
		},
		methods: {
			loadNewsList:function(type){	
				//type add 加载更多 refresh下拉刷新
				if(type === 'add'){
					if(this.loadMoreStatus === 2){
						return;
					}
					this.loadMoreStatus = 1;
				}
				if(type === 'refresh'){
					this.page = 1;
					this.newsList = []; //刷新前清空数组
				}
				//下拉刷新 关闭刷新动画
				
				//获取基础参数
				let page = this.page;
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['keyword='+this.keyword,'page='+page,'timestamp='+timestamp]);
				this.helper.req({
					api:"getNewsSearch",
					timestamp:timestamp,
					data:{keyword:this.keyword,page:page},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					if(type === 'refresh'){
						this.$refs.mixPulldownRefresh;
						this.$refs.mixPulldownRefresh.endPulldownRefresh();
						this.loadMoreStatus = 0;
						
						uni.stopPullDownRefresh();
					}
					let current_page = res.data.data.list.current_page;
					let last_page = res.data.data.list.last_page;
					if(current_page >= last_page){
						this.loadMoreStatus = 2;
					}else{
						this.loadMoreStatus = 0;
					} 
					this.newsList = this.newsList.concat(res.data.data.list.data);
					this.countResult = '共搜索到'+res.data.data.list.total+'个结果';
					if(res.data.data.list.total == 0){
						this.countResult = "未搜索到结果!";
					}
				})
			},
			//新闻详情
			navToDetails(item){
				uni.navigateTo({
					url: `/pages/news/detail?id=${item.id}`
				})
			},
			
			//下拉刷新
			onPulldownReresh(){
				this.loadNewsList('refresh');
			},
			//上滑加载
			loadMore(){
				this.page++;
				this.loadNewsList('add');
			},
			setEnableScroll(enable){
				if(this.enableScroll !== enable){
					this.enableScroll = enable;
				}
			},

			//链接
			toUrl:function(url){
				// console.log(url);
				uni.navigateTo({
					url: url,
				});	
			},
			
		},
    }
</script>

<style lang="scss">
page, .content{
	background-color: #f8f8f8;
	height: 100%;
	overflow: hidden;
}
.panel-scroll-box{
	height: 100%;
	
	.panel-item{
		background: #fff;
		padding: 30px 0;
		border-bottom: 2px solid #000;
	}
}
.count_result{
	font-size: 28upx;
	line-height: 32upx;
	color: #333;
	text-align: center;
	padding: 20upx;
}
</style>
